define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                 shop_id:0,
                 goods:[],
                 ids:{},
                  tags:[]
                };
            },
            el: '#pageChooseGoods',
            created: function() {
                this.fetchData();
            },
              mounted: function() {
                if (this.mounted == 1) {
                    return;
                }
                this.mounted = 1;
            },

            methods: {
                fetchData: function() {
                    var _this = this;
                    this.shop_id = this.$route.params.shop_id;
                    this.id = this.$route.params.id;
                    this.loadids();
                    this.getTags();
                    
                },
                loadids:function(){
                    var that = this ;
                    that.goods=[];
                    var params =  {
                      shop_id:this.shop_id,
                      limit:100,
                      attr_id:this.id,
                      status:1,
                      fields:'goods_name,attr_id,id,default_img',
                    };
                    
                    app.get("mall/goods/get",params, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      var ids ={};
                      for (var i in res.data.list) {
                          ids[res.data.list[i].id] = res.data.list[i].id;
                      }
                      that.ids =ids; 
                      that.loadGoods();
                      
                    });
                },

                loadGoods:function(){
                    var that = this ;
                    that.goods=[];
                    var params =  {
                      shop_id:this.shop_id,
                      limit:100,
                      fields:'goods_name,attr_id,id,default_img',
                    };
                    if(this.attr_id){
                        params.attr_id = this.attr_id;
                    }
                    if(this.kw){
                        params.kw = this.kw;
                    }
                    app.get("mall/goods/get",params, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      var goods =[];
                      var good={};
                      for (var i in res.data.list) {
                            good = res.data.list[i];
                            if(that.ids[good.id]){
                               good.choose=1; 
                            }
                            goods.push(good);
                        }
                        that.goods = goods; 
                    });
                },
                tag:function(e,goods_id){
                   
                    if($(e.target).prop("checked")){
                        this.addTag(goods_id);
                    }else{
                        this.delTag(goods_id);
                    }
                },

                delTag:function(goods_id){

                    var that = this ;
                    app.get("mall/goods/delTag", {
                      goods_id : goods_id,
                      shop_id:this.shop_id,
                      attr_id:this.id
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.ids[goods_id]=0;
                       app.setPageData("index",{reload:1});
                    });
                },

                addTag:function(goods_id){

                    var that = this ;
                    app.get("mall/goods/addTag", {
                      goods_id : goods_id,
                      shop_id:this.shop_id,
                      attr_id:this.id
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.ids[goods_id]=goods_id;
                       app.setPageData("index",{reload:1});
                    });
                },
                getTags:function(){
                   var that = this ;
                    app.get("mall/tag/get", {
                      shop_id:this.shop_id
                    }, function (res) {
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      that.tags = res.data.list; 
                     Vue.nextTick(function(){
                        that.select();
                     });
                     
                    });
                },
                select:function(){
                  var that = this;
                  var $eventSelect =$("#pageChooseGoods #tag_name");
                  $eventSelect.select2({
                      placeholder: '请选择分类名称或商品名称',
                      allowClear: false,
                      tags: true,

                  });
                 $eventSelect.on("select2:select", function(e) {
                      var data = $(this).select2("data")[0];
                      if(Number(data.id)>0){
                        that.attr_id = data.id;
                        that.kw ='';
                      }else{
                        that.attr_id = 0;
                        that.kw = data.id;
                      }
                      console.log(data);
                      that.loadGoods();
                   });

                }
                


            }

        }).$mount('#app');
    };
    module.exports = controller;
});
